package com.managerConsummer.controller;

import com.alibaba.fastjson.JSON;
import com.managerConsummer.service.RoleServiceFeign;
import com.managerConsummer.service.UserServiceFeign;
import entity.ResponseMessage;
import entity.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import util.FileUpload;

import javax.servlet.http.HttpServletResponse;

/**
 * 用户控制器
 * The type User controller.
 *
 * @author tapoliuliyue
 * @date 2022 /04/23
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    /**
     * The User service feign.
     */
    @Autowired
    private UserServiceFeign userServiceFeign;
    /**
     * The Role service feign.
     */
    @Autowired
    private RoleServiceFeign roleServiceFeign;
    /**
     * The Redis template.
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * The Logger.
     */
    private Logger logger = Logger.getLogger(this.getClass());

    /**
     * Find all user string.
     *
     * @param user the user
     * @return the string
     */
    @RequestMapping("/findAllUser")
    public String findAllUser(@RequestBody User user) {
        logger.info("UserController findAllUser params:" + user);
        String allUser = userServiceFeign.findAllUser(user);
        logger.debug("UserController findAllUser params:" + user + ",result" + allUser);
        return allUser;
    }

    /**
     * Insert user string.
     *
     * @param user the user
     * @return the string
     */
    @RequestMapping("/insertUser")
    public String insertUser(@RequestBody User user) {
        logger.info("UserController insertUser params:" + user);
        String s = userServiceFeign.insertUser(user);
        logger.debug("UserController insertUser params:" + user + ",result" + s);
        return s;
    }

    /**
     * Update user string.
     *
     * @param user the user
     * @return the string
     */
    @RequestMapping("/updateUser")
    public String updateUser(@RequestBody User user) {
        logger.info("UserController updateUser params:" + user);
        String s = userServiceFeign.updateUser(user);
        logger.debug("UserController updateUser params:" + user + ",result" + s);
        return s;
    }

    /**
     * Delete by id string.
     *
     * @param id the id
     * @return the string
     */
    @RequestMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") String id) {
        logger.info("UserController deleteById params:" + id);
        String s = userServiceFeign.deleteById(id);
        logger.debug("UserController deleteById params:" + id + ",result" + s);
        return s;
    }

    /**
     * Upload string.
     *
     * @param multipartFile the multipart file
     * @return the string
     */
    @RequestMapping("/upload")
    public String upload(@RequestParam(name = "file") MultipartFile multipartFile) {
        ResponseMessage msg = FileUpload.fileUpload(multipartFile);
        return JSON.toJSONString(msg);
    }

    /**
     * Gets redis user.
     *
     * @return the redis user
     */
    @RequestMapping("/getRedisUser/{userName}")
    public String getRedisUser(HttpServletResponse response, @PathVariable("userName") String userName) {
        ValueOperations<String, User> operations = redisTemplate.opsForValue();
        User user = operations.get(userName);
        String allUser = userServiceFeign.findAllUser(user);
        return JSON.toJSONString(user);
    }

    /**
     * Select one user role power string.
     *
     * @param user the user
     * @return the string
     */
    @RequestMapping("/selectOneUserRolePower")
    public String selectOneUserRolePower(@RequestBody User user) {
        logger.info("UserController selectOneUserRolePower params:" + user);
        String s = userServiceFeign.selectOneUserRolePower(user);
        logger.debug("UserController selectOneUserRolePower params:" + user + ",result" + s);
        return s;
    }

}
